package cn.com.surker.production.controller;

import cn.com.surker.base.BaseEntity;
import cn.com.surker.cache.MemoryCache;
import cn.com.surker.common.Code;
import cn.com.surker.enums.ICodeDescEnum;
import cn.com.surker.enums.production.car.CarWorkStatusEnum;
import cn.com.surker.handler.DisCoordinator;
import cn.com.surker.production.entity.TranCar;
import cn.com.surker.production.entity.vo.TranCarVo;
import cn.com.surker.production.service.ErpCarService;
import cn.com.surker.spring.RestResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.RollbackException;
import java.util.ArrayList;
import java.util.List;

/**
 * 运输车辆相关操作
 *
 * @author luotao
 * @date 2024/9/3
 */
@RestController
@RequestMapping("/pro/tran/car")
public class TranCarContrller extends ProBaseController {

	@Resource
	private ErpCarService tranCarService;
	@Autowired
	private DisCoordinator disCoordinator;

	private MemoryCache memoryCache = MemoryCache.getInstance();

	/**
	 * 车辆上号
	 *
	 * @param carId  车辆id
	 * @param driver 司机名称
	 * @return
	 */
	@RequestMapping(value = "/clock/{carId}", method = RequestMethod.PUT)
	@ResponseBody
	@Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
	public RestResultDto clock(@PathVariable Long carId, Integer carStatus, String driver) {
		if (carId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		CarWorkStatusEnum anEnum = ICodeDescEnum.getEnum(CarWorkStatusEnum.class, carStatus);
		if (anEnum == null) {
			return RestResultDto.fail("车辆状态非法");
		}
		//	查询车辆
		TranCar tranCar = tranCarService.getById(carId);
		if (!BaseEntity.validateExist(tranCar)) {
			return RestResultDto.fail("车辆不存在或者已经被禁用");
		}
		memoryCache.remove(DisCoordinator.allCarKey);
		tranCarService.updateStatus(orgId, carId, carStatus, true, null);
		compInvByCar(carId);
		return RestResultDto.success();
	}

	/**
	 * 车辆置为休息
	 *
	 * @param carId
	 * @param driver
	 * @return
	 *//*
	@RequestMapping(value = "/offline/{carId}", method = RequestMethod.PUT)
	@ResponseBody
	@Transactional(rollbackFor = RollbackException.class, propagation = Propagation.REQUIRED)
	public RestResultDto offline(@PathVariable Long carId, String driver) {
		if (carId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		//	查询车辆
		TranCar tranCar = tranCarService.getById(carId);
		if (!BaseEntity.validateExist(tranCar)) {
			return RestResultDto.fail("车辆不存在或者已经被禁用");
		}
		tranCarService.updateStatus(carId, CarWorkStatusEnum.REST.getCode());
		compInvByCar(carId);
		return RestResultDto.success();
	}*/

	/**
	 * 查询站点下的全部车辆
	 *
	 * @param re    是否需要立即刷新,1为需要
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto list(Integer re) {
		if (re != null && re.equals(1)) {
			Object o = disCoordinator.carList();
			return RestResultDto.success(o);
		}
		Object o = null;
		try {
			o = memoryCache.get(DisCoordinator.allCarKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (o == null) {
			o = disCoordinator.carList();
		}
		return RestResultDto.success(o);
	}

	/**
	 * 查询排队等待的车队列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/queue/list", method = RequestMethod.POST)
	@ResponseBody
	@Deprecated
	public RestResultDto List() {
		List<Integer> carStatusList = new ArrayList<>();
		carStatusList.add(CarWorkStatusEnum.WAIT.getCode());
	/*	carStatusList.add(CarWorkStatusEnum.PRO.getCode());
		carStatusList.add(CarWorkStatusEnum.REST.getCode());
		carStatusList.add(CarWorkStatusEnum.PRO_COMPLETE.getCode());
		carStatusList.add(CarWorkStatusEnum.GO_OUT.getCode());
		carStatusList.add(CarWorkStatusEnum.GO_BACK.getCode());
		carStatusList.add(CarWorkStatusEnum.WR.getCode());*/
		List<TranCarVo> list = tranCarService.findOrgList(orgId, carStatusList, "sign_time");
		return RestResultDto.success(list);
	}


	/**
	 * 专用于查询运输和等待之外的车辆
	 *
	 * @return
	 */
	/*@RequestMapping(value = "/other/list", method = RequestMethod.POST)
	@ResponseBody*/
	@Deprecated
	public RestResultDto otherList() {
		List<Integer> carStatusList = new ArrayList<>();
		carStatusList.add(CarWorkStatusEnum.REST.getCode());
		List<TranCarVo> list = tranCarService.findOrgList(orgId, carStatusList, null);
		return RestResultDto.success(list);
	}

	/**
	 * 查询配额票的车辆
	 *
	 * @return
	 */
	@RequestMapping(value = "/quota", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto quotaList() {
		// List<TranCar> list = tranCarService.findOrgList(orgId, null, "sign_time");
		List<TranCarVo> list = tranCarService.findQuotaList(orgId);
		return RestResultDto.success(list);
	}

}
